#include <iostream>
#include <QMouseEvent>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <mysql/mysql.h>
#include <stdint.h>
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "socketandssl.h"
#include <cstdio>
#include "user_interact_window.h"

#define OPEN 1;
#define CLOSE 0;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setWindowFlag(Qt::FramelessWindowHint);

    //设置窗口透明化
    setAttribute(Qt::WA_TranslucentBackground);

    ui->Login_Button->setStyleSheet("QPushButton {background-color:rgba(20,120,180,60);"
                                        "border-radius:10px;"
                                        "border:0;}"

                                        "QPushButton:hover {background-color:rgba(20,120,180,90);"
                                        "border-radius:10px;"
                                        "border:0;}");

    ui->Register_Button->setStyleSheet("QPushButton {background-color:rgba(20,120,180,60);"
                                        "border-radius:10px;"
                                        "border:0;}"

                                        "QPushButton:hover {background-color:rgba(20,120,180,90);"
                                        "border-radius:10px;"
                                        "border:0;}");

    ui->help_pushButton->setStyleSheet("QPushButton {color: #007bff;"
                                        "text-decoration: underline;"
                                        "background-color:rgba(0,0,0,0);"
                                        "border:0;}"

                                        "QPushButton:hover {color: #cf7bff;"
                                        "text-decoration: underline;"
                                        "background-color:rgba(0,0,0,0);"
                                        "border:0;}");

    ui->password_visible_pushButton->setStyleSheet("QPushButton {border:0;"
                                                   "background-image:url(:/icon/eye_close_fixed.jpg);"
                                                   "background-color:rgba(0,0,0,0);"
                                                   "border-radius:10px;}");


    ui->Quit_pushButton->setStyleSheet("QPushButton {border:0;"
                                       "border-radius:10px;"
                                       "background-color:#ff4500;}"

                                       "QPushButton:hover {border:0;"
                                       "border-radius:10px;"
                                       "background-color:#ff0000;}");

    ui->Minimize_pushButton->setStyleSheet("QPushButton {border:0;"
                                       "border-radius:10px;"
                                       "background-color:#ffffff;"
                                       "color:#000000;"
                                       "padding-top:5px;}"

                                       "QPushButton:hover {border:0;"
                                       "border-radius:10px;"
                                       "background-color:#dddddd;}");

    ui->compelet_Register_Button->setStyleSheet("QPushButton {background-color:rgba(20,180,120,60);"
                                                "border-radius:10px;"
                                                "border:0;}"

                                                "QPushButton:hover {background-color:rgba(20,180,120,80);"
                                                "border-radius:10px;"
                                                "border:0;}");

    ui->backToNameMaking_pushButton->setStyleSheet("QPushButton {font-size: 20px;"
                                                "background-color:rgba(50,50,50,110);"
                                                "border-radius:15px;}"

                                                   "QPushButton:hover {font-size: 20px;"
                                                   "background-color:rgba(100,100,100,110);"
                                                   "border-radius:15px;}");

    ui->password_lineEdit->setEchoMode(QLineEdit::Password);   
    ui->register_password_lineEdit->setEchoMode(QLineEdit::Password);

    Register_QPropertyAnimation_1 = new QPropertyAnimation(ui->username_lineEdit,"geometry");
    Register_QPropertyAnimation_1->setDuration(1000);
    Register_QPropertyAnimation_1->setStartValue(QRect(ui->username_lineEdit->pos().x(),ui->username_lineEdit->pos().y(),ui->username_lineEdit->width(),ui->username_lineEdit->height()));
    Register_QPropertyAnimation_1->setEndValue(QRect(ui->username_lineEdit->pos().x() + 600,ui->username_lineEdit->pos().y(),ui->username_lineEdit->width(),ui->username_lineEdit->height()));

    Register_QPropertyAnimation_2 = new QPropertyAnimation(ui->password_lineEdit,"geometry");
    Register_QPropertyAnimation_2->setDuration(1100);
    Register_QPropertyAnimation_2->setStartValue(QRect(ui->password_lineEdit->pos().x(),ui->password_lineEdit->pos().y(),ui->password_lineEdit->width(),ui->password_lineEdit->height()));
    Register_QPropertyAnimation_2->setEndValue(QRect(ui->password_lineEdit->pos().x() + 600,ui->password_lineEdit->pos().y(),ui->password_lineEdit->width(),ui->password_lineEdit->height()));

    Register_QPropertyAnimation_3 = new QPropertyAnimation(ui->password_visible_pushButton,"geometry");
    Register_QPropertyAnimation_3->setDuration(1100);
    Register_QPropertyAnimation_3->setStartValue(QRect(ui->password_visible_pushButton->pos().x(),ui->password_visible_pushButton->pos().y(),ui->password_visible_pushButton->width(),ui->password_visible_pushButton->height()));
    Register_QPropertyAnimation_3->setEndValue(QRect(ui->password_visible_pushButton->pos().x() + 600,ui->password_visible_pushButton->pos().y(),ui->password_visible_pushButton->width(),ui->password_visible_pushButton->height()));

    Register_QPropertyAnimation_4 = new QPropertyAnimation(ui->Login_Button,"geometry");
    Register_QPropertyAnimation_4->setDuration(1000);
    Register_QPropertyAnimation_4->setStartValue(QRect(ui->Login_Button->pos().x(),ui->Login_Button->pos().y(),ui->Login_Button->width(),ui->Login_Button->height()));
    Register_QPropertyAnimation_4->setEndValue(QRect(ui->Login_Button->pos().x(),ui->Login_Button->pos().y() + 120,ui->Login_Button->width(),ui->Login_Button->height()));

    Register_QPropertyAnimation_5 = new QPropertyAnimation(ui->register_username_lineEdit,"geometry");
    Register_QPropertyAnimation_5->setDuration(1000);
    Register_QPropertyAnimation_5->setStartValue(QRect(ui->register_username_lineEdit->pos().x(),ui->register_username_lineEdit->pos().y(),ui->register_username_lineEdit->width(),ui->register_username_lineEdit->height()));
    Register_QPropertyAnimation_5->setEndValue(QRect(ui->register_username_lineEdit->pos().x(),ui->register_username_lineEdit->pos().y() - 400,ui->register_username_lineEdit->width(),ui->register_username_lineEdit->height()));

    Register_QPropertyAnimation_6 = new QPropertyAnimation(ui->Login_Button,"windowOpacity");
    Register_QPropertyAnimation_6->setDuration(1000);
    Register_QPropertyAnimation_6->setStartValue(0);
    Register_QPropertyAnimation_6->setEndValue(1);

    QGraphicsOpacityEffect *register_username_lineEdit_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *OK_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *OKorREJECT_tip_label_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *REJECT_label_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *register_tip_label_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *label_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *backToLogin_Tip_label_show_up = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *Login_Button_quit = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *Register_Button_quit = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *register_username_lineEdit_quit = new QGraphicsOpacityEffect(this);
    QGraphicsOpacityEffect *compelet_Register_Button_show_up = new QGraphicsOpacityEffect(this);

    register_username_lineEdit_show_up->setOpacity(1);
    OK_show_up->setOpacity(0);
    OKorREJECT_tip_label_show_up->setOpacity(0);
    REJECT_label_show_up->setOpacity(0);

    ui->register_username_lineEdit->setGraphicsEffect(register_username_lineEdit_show_up);
    ui->register_tip_label->setGraphicsEffect(register_tip_label_show_up);
    ui->label->setGraphicsEffect(label_show_up);
    ui->backToLogin_Tip_label->setGraphicsEffect(backToLogin_Tip_label_show_up);
    ui->OKorREJECT_tip_label->setGraphicsEffect(OKorREJECT_tip_label_show_up);
    ui->REJECT_label->setGraphicsEffect(REJECT_label_show_up);
    ui->OK_label->setGraphicsEffect(OK_show_up);
    ui->register_username_lineEdit->setGraphicsEffect(register_username_lineEdit_quit);
    ui->Login_Button->setGraphicsEffect(Login_Button_quit);
    ui->Register_Button->setGraphicsEffect(Register_Button_quit);
    ui->compelet_Register_Button->setGraphicsEffect(compelet_Register_Button_show_up);

    Register_QPropertyAnimation_15 = new QPropertyAnimation(ui->label, "geometry");
    Register_QPropertyAnimation_15->setDuration(1000);
    Register_QPropertyAnimation_15->setStartValue(QRect(120, 510, 381, 41));
    Register_QPropertyAnimation_15->setEndValue(QRect(120, 510, 0, 0));
    Register_QPropertyAnimation_15->setEasingCurve(QEasingCurve::InOutQuad);



    //大小变化动画
    Register_QPropertyAnimation_7 = new QPropertyAnimation(ui->register_username_lineEdit, "geometry");
    Register_QPropertyAnimation_7->setDuration(1000);
    Register_QPropertyAnimation_7->setStartValue(QRect(120, 270, 0, 0));
    Register_QPropertyAnimation_7->setEndValue(QRect(120, 270, 341, 41));
    Register_QPropertyAnimation_7->setEasingCurve(QEasingCurve::InOutQuad);

    //透明度变化动画
    Register_QPropertyAnimation_8 = new QPropertyAnimation(register_username_lineEdit_show_up, "opacity");
    Register_QPropertyAnimation_8->setDuration(1500);
    Register_QPropertyAnimation_8->setStartValue(0);
    Register_QPropertyAnimation_8->setEndValue(1);

    Register_QPropertyAnimation_9 = new QPropertyAnimation(label_show_up, "opacity");
    Register_QPropertyAnimation_9->setDuration(1500);
    Register_QPropertyAnimation_9->setStartValue(1);
    Register_QPropertyAnimation_9->setEndValue(0);

    Register_QPropertyAnimation_10 = new QPropertyAnimation(ui->Register_Button,"geometry");
    Register_QPropertyAnimation_10->setDuration(1000);
    Register_QPropertyAnimation_10->setStartValue(QRect(ui->Register_Button->pos().x(),ui->Register_Button->pos().y(),ui->Register_Button->width(),ui->Register_Button->height()));
    Register_QPropertyAnimation_10->setEndValue(QRect(ui->Register_Button->pos().x(),ui->Register_Button->pos().y() - 210,ui->Register_Button->width(),ui->Register_Button->height()));

    Register_QPropertyAnimation_11 = new QPropertyAnimation(register_tip_label_show_up, "opacity");
    Register_QPropertyAnimation_11->setDuration(1500);
    Register_QPropertyAnimation_11->setStartValue(0);
    Register_QPropertyAnimation_11->setEndValue(1);

    Register_QPropertyAnimation_12 = new QPropertyAnimation(ui->register_tip_label, "geometry");
    Register_QPropertyAnimation_12->setDuration(1500);
    Register_QPropertyAnimation_12->setStartValue(QRect(125, 230, 0, 0));
    Register_QPropertyAnimation_12->setEndValue(QRect(125, 230, 341, 41));
    Register_QPropertyAnimation_12->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_13 = new QPropertyAnimation(ui->backToLogin_Tip_label, "geometry");
    Register_QPropertyAnimation_13->setDuration(1500);
    Register_QPropertyAnimation_13->setStartValue(QRect(125, 475, 0, 0));
    Register_QPropertyAnimation_13->setEndValue(QRect(125, 475, 341, 41));
    Register_QPropertyAnimation_13->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_14 = new QPropertyAnimation(backToLogin_Tip_label_show_up, "opacity");
    Register_QPropertyAnimation_14->setDuration(1500);
    Register_QPropertyAnimation_14->setStartValue(0);
    Register_QPropertyAnimation_14->setEndValue(1);

    Register_QPropertyAnimation_13 = new QPropertyAnimation(ui->backToLogin_Tip_label, "geometry");
    Register_QPropertyAnimation_13->setDuration(1500);
    Register_QPropertyAnimation_13->setStartValue(QRect(125, 475, 0, 0));
    Register_QPropertyAnimation_13->setEndValue(QRect(125, 475, 341, 41));
    Register_QPropertyAnimation_13->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_16 = new QPropertyAnimation(ui->change_Icon_pushButton, "geometry");
    Register_QPropertyAnimation_16->setDuration(1500);
    Register_QPropertyAnimation_16->setStartValue(QRect(800, 190, 31, 31));
    Register_QPropertyAnimation_16->setEndValue(QRect(331, 190, 31, 31));
    Register_QPropertyAnimation_16->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_17 = new QPropertyAnimation(ui->OK_label, "geometry");
    Register_QPropertyAnimation_17->setDuration(1000);
    Register_QPropertyAnimation_17->setStartValue(QRect(290, 335, 41, 41));
    Register_QPropertyAnimation_17->setEndValue(QRect(425, 335, 41, 41));
    Register_QPropertyAnimation_17->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_22 = new QPropertyAnimation(OK_show_up, "opacity");
    Register_QPropertyAnimation_22->setDuration(1000);
    Register_QPropertyAnimation_22->setStartValue(0);
    Register_QPropertyAnimation_22->setEndValue(1);

    Register_QPropertyAnimation_18 = new QPropertyAnimation(ui->OKorREJECT_tip_label, "geometry");
    Register_QPropertyAnimation_18->setDuration(1000);
    Register_QPropertyAnimation_18->setStartValue(QRect(285, 340, 171, 31));
    Register_QPropertyAnimation_18->setEndValue(QRect(285, 340, 171, 31));
    Register_QPropertyAnimation_18->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_19 = new QPropertyAnimation(OKorREJECT_tip_label_show_up, "opacity");
    Register_QPropertyAnimation_19->setDuration(1000);
    Register_QPropertyAnimation_19->setStartValue(0);
    Register_QPropertyAnimation_19->setEndValue(1);

    Register_QPropertyAnimation_20 = new QPropertyAnimation(ui->REJECT_label, "geometry");
    Register_QPropertyAnimation_20->setDuration(1000);
    Register_QPropertyAnimation_20->setStartValue(QRect(290, 332, 41, 41));
    Register_QPropertyAnimation_20->setEndValue(QRect(425, 332, 41, 41));
    Register_QPropertyAnimation_20->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_21 = new QPropertyAnimation(REJECT_label_show_up, "opacity");
    Register_QPropertyAnimation_21->setDuration(1000);
    Register_QPropertyAnimation_21->setStartValue(0);
    Register_QPropertyAnimation_21->setEndValue(1);

    Register_QPropertyAnimation_23 = new QPropertyAnimation(ui->label, "geometry");
    Register_QPropertyAnimation_23->setDuration(1000);
    Register_QPropertyAnimation_23->setStartValue(QRect(120, 580, 381, 41));
    Register_QPropertyAnimation_23->setEndValue(QRect(-380, 580, 41, 41));
    Register_QPropertyAnimation_23->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_24 = new QPropertyAnimation(ui->Login_Button, "geometry");
    Register_QPropertyAnimation_24->setDuration(1000);
    Register_QPropertyAnimation_24->setStartValue(QRect(120, 510, 141, 31));
    Register_QPropertyAnimation_24->setEndValue(QRect(-380, 510, 141, 31));
    Register_QPropertyAnimation_24->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_25 = new QPropertyAnimation(ui->backToNameMaking_pushButton, "geometry");
    Register_QPropertyAnimation_25->setDuration(1000);
    Register_QPropertyAnimation_25->setStartValue(QRect(-50, 60, 31, 31));
    Register_QPropertyAnimation_25->setEndValue(QRect(10, 60, 31, 31));
    Register_QPropertyAnimation_25->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_26 = new QPropertyAnimation(ui->register_password_lineEdit, "geometry");
    Register_QPropertyAnimation_26->setDuration(1000);
    Register_QPropertyAnimation_26->setStartValue(QRect(620, 270, 341, 41));
    Register_QPropertyAnimation_26->setEndValue(QRect(120, 270, 341, 41));
    Register_QPropertyAnimation_26->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_27 = new QPropertyAnimation(ui->register_password_visible_pushButton, "geometry");
    Register_QPropertyAnimation_27->setDuration(1000);
    Register_QPropertyAnimation_27->setStartValue(QRect(620, 280, 21, 21));
    Register_QPropertyAnimation_27->setEndValue(QRect(430, 280, 21, 21));
    Register_QPropertyAnimation_27->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_28 = new QPropertyAnimation(ui->register_username_lineEdit, "geometry");
    Register_QPropertyAnimation_28->setDuration(1000);
    Register_QPropertyAnimation_28->setStartValue(QRect(120, 270, 341, 41));
    Register_QPropertyAnimation_28->setEndValue(QRect(-380, 270, 341, 41));
    Register_QPropertyAnimation_28->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_29 = new QPropertyAnimation(ui->backToLogin_Tip_label, "geometry");
    Register_QPropertyAnimation_29->setDuration(1000);
    Register_QPropertyAnimation_29->setStartValue(QRect(125, 475, 341, 41));
    Register_QPropertyAnimation_29->setEndValue(QRect(-375, 475, 341, 41));
    Register_QPropertyAnimation_29->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_30 = new QPropertyAnimation(ui->Register_Button, "geometry");
    Register_QPropertyAnimation_30->setDuration(1000);
    Register_QPropertyAnimation_30->setStartValue(QRect(125, 340, 141, 31));
    Register_QPropertyAnimation_30->setEndValue(QRect(-375, 340, 141, 31));
    Register_QPropertyAnimation_30->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_31 = new QPropertyAnimation(Login_Button_quit, "opacity");
    Register_QPropertyAnimation_31->setDuration(1000);
    Register_QPropertyAnimation_31->setStartValue(1);
    Register_QPropertyAnimation_31->setEndValue(0);

    Register_QPropertyAnimation_32 = new QPropertyAnimation(Register_Button_quit, "opacity");
    Register_QPropertyAnimation_32->setDuration(1000);
    Register_QPropertyAnimation_32->setStartValue(1);
    Register_QPropertyAnimation_32->setEndValue(0);

    Register_QPropertyAnimation_33 = new QPropertyAnimation(register_username_lineEdit_quit, "opacity");
    Register_QPropertyAnimation_33->setDuration(1000);
    Register_QPropertyAnimation_33->setStartValue(1);
    Register_QPropertyAnimation_33->setEndValue(0);

    Register_QPropertyAnimation_34 = new QPropertyAnimation(ui->compelet_Register_Button, "geometry");
    Register_QPropertyAnimation_34->setDuration(1500);
    Register_QPropertyAnimation_34->setStartValue(QRect(1000, 340, 141, 31));
    Register_QPropertyAnimation_34->setEndValue(QRect(120, 340, 141, 31));
    Register_QPropertyAnimation_34->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_35 = new QPropertyAnimation(compelet_Register_Button_show_up, "opacity");
    Register_QPropertyAnimation_35->setDuration(1500);
    Register_QPropertyAnimation_35->setStartValue(0);
    Register_QPropertyAnimation_35->setEndValue(1);

    Register_QPropertyAnimation_36 = new QPropertyAnimation(ui->OK_label, "geometry");
    Register_QPropertyAnimation_36->setDuration(1500);
    Register_QPropertyAnimation_36->setStartValue(QRect(290, 385, 41, 41));
    Register_QPropertyAnimation_36->setEndValue(QRect(425, 385, 41, 41));
    Register_QPropertyAnimation_36->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_37 = new QPropertyAnimation(ui->OKorREJECT_tip_label, "geometry");
    Register_QPropertyAnimation_37->setDuration(1500);
    Register_QPropertyAnimation_37->setStartValue(QRect(290, 390, 0, 31));
    Register_QPropertyAnimation_37->setEndValue(QRect(290, 390, 166, 31));
    Register_QPropertyAnimation_37->setEasingCurve(QEasingCurve::InOutQuad);

    Register_QPropertyAnimation_38 = new QPropertyAnimation(ui->REJECT_label, "geometry");
    Register_QPropertyAnimation_38->setDuration(1500);
    Register_QPropertyAnimation_38->setStartValue(QRect(290, 383, 41, 41));
    Register_QPropertyAnimation_38->setEndValue(QRect(425, 383, 41, 41));
    Register_QPropertyAnimation_38->setEasingCurve(QEasingCurve::InOutQuad);



    sqlite3 *db;
        rc =sqlite3_open("/home/ubuntu/Desktop/socket/database_for_socket", &db);
        if(rc != SQLITE_OK){
            //fprintf(stderr,"cannot open database: %s\n",sqlite3_errmsg(db));
            //sqlite3_close(db);
        }
    const char *sql = "SELECT username, password FROM userinfo;";
    rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);

    if(rc != SQLITE_OK){
        fprintf(stderr, "cannot execute statement : %s\n", (char *)sqlite3_close(db));
    }
    printf("Sqlite3 database data trans to local server compeleted. \nAdministrator,\033[0;31mwelcome back!\033[0m\n");


}


MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_Login_Button_clicked()
{
    if(!registerOrLogin_stat){
        Register_QPropertyAnimation_1->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_2->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_3->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_4->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_5->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_6->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_7->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_8->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_9->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_10->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_11->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_12->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_13->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_14->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_15->setDirection(QAbstractAnimation::Backward);
        Register_QPropertyAnimation_16->setDirection(QAbstractAnimation::Backward);


        Register_QPropertyAnimation_1->start();
        Register_QPropertyAnimation_2->start();
        Register_QPropertyAnimation_3->start();
        Register_QPropertyAnimation_4->start();
        Register_QPropertyAnimation_5->start();
        Register_QPropertyAnimation_6->start();
        Register_QPropertyAnimation_7->start();
        Register_QPropertyAnimation_8->start();
        Register_QPropertyAnimation_9->start();
        Register_QPropertyAnimation_10->start();
        Register_QPropertyAnimation_11->start();
        Register_QPropertyAnimation_12->start();
        Register_QPropertyAnimation_13->start();
        Register_QPropertyAnimation_14->start();
        Register_QPropertyAnimation_15->start();
        Register_QPropertyAnimation_16->start();

        ui->Register_Button->setText("Register");


        registerOrLogin_stat = 1;
    }else{
        QString username = ui->username_lineEdit->text();
        QByteArray username_ByteArray = username.toLatin1();
        char *username_char_arr = username_ByteArray.data();

        QString password = ui->password_lineEdit->text();
        QByteArray password_ByteArray = password.toLatin1();
        char *password_char_arr = password_ByteArray.data();



        if(username.isEmpty() && !password.isEmpty()){
            ui->Tips_Column->textlabel->setText("Please enter username");
            ui->Tips_Column->AnimationStart();
        }else if(password.isEmpty() && !username.isEmpty()){
            ui->Tips_Column->textlabel->setText("Please enter password");
            ui->Tips_Column->AnimationStart();
        }else if(username.isEmpty() && password.isEmpty()){
            ui->Tips_Column->textlabel->setText("Please enter username and password");
            ui->Tips_Column->AnimationStart();
        }else{
            int client_socket = socketAndSSL::Creat_client_socket();

            socketAndSSL::Connet_to_server(client_socket);

            SSL *ssl = socketAndSSL::Sync_init_ssl(client_socket);

            socketAndSSL::Ssl_connect(client_socket, ssl);

            char buffer[1024];
            memset(buffer, 0, sizeof(buffer));

            int ret = socketAndSSL::Login_request(buffer, ssl, username_char_arr, password_char_arr, 1024);

            if(ret == 1){
                //success
                ui->OKorREJECT_tip_label->setText("Welcome !");

                static int current_end_animation = 0;

                Register_QPropertyAnimation_36->setDirection(QAbstractAnimation::Forward);
                Register_QPropertyAnimation_37->setDirection(QAbstractAnimation::Forward);
                Register_QPropertyAnimation_22->setDirection(QAbstractAnimation::Forward);
                Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Forward);


                Register_QPropertyAnimation_36->start();
                Register_QPropertyAnimation_37->start();
                Register_QPropertyAnimation_22->start();
                Register_QPropertyAnimation_19->start();

                QTimer *timer = new QTimer(this);
                timer->start(3000);

                current_end_animation++;

                connect(timer,&QTimer::timeout,this,[=]{
                    if(current_end_animation == 1){
                        Register_QPropertyAnimation_36->setDirection(QAbstractAnimation::Backward);
                        Register_QPropertyAnimation_37->setDirection(QAbstractAnimation::Backward);
                        Register_QPropertyAnimation_22->setDirection(QAbstractAnimation::Backward);
                        Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Backward);


                        Register_QPropertyAnimation_36->start();
                        Register_QPropertyAnimation_37->start();
                        Register_QPropertyAnimation_22->start();
                        Register_QPropertyAnimation_19->start();

                        timer->stop();
                        delete timer;
                        current_end_animation--;
                    }else{
                        current_end_animation--;
                    }

                    user_Interact_Window *user_interact_window = new user_Interact_Window;
                    user_interact_window->setWindowFlag(Qt::FramelessWindowHint);

                    //设置窗口透明化
                    setAttribute(Qt::WA_TranslucentBackground);
                    user_interact_window->show();

                    this->close();
                });
            }else{
                //fail
                ui->OKorREJECT_tip_label->setText("Bad passOrName");

                static int current_end_animation = 0;

                Register_QPropertyAnimation_38->setDirection(QAbstractAnimation::Forward);
                Register_QPropertyAnimation_37->setDirection(QAbstractAnimation::Forward);
                Register_QPropertyAnimation_21->setDirection(QAbstractAnimation::Forward);
                Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Forward);


                Register_QPropertyAnimation_38->start();
                Register_QPropertyAnimation_37->start();
                Register_QPropertyAnimation_21->start();
                Register_QPropertyAnimation_19->start();

                QTimer *timer = new QTimer(this);
                timer->start(3000);

                current_end_animation++;

                connect(timer,&QTimer::timeout,this,[=]{
                    if(current_end_animation == 1){
                        Register_QPropertyAnimation_38->setDirection(QAbstractAnimation::Backward);
                        Register_QPropertyAnimation_37->setDirection(QAbstractAnimation::Backward);
                        Register_QPropertyAnimation_21->setDirection(QAbstractAnimation::Backward);
                        Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Backward);


                        Register_QPropertyAnimation_38->start();
                        Register_QPropertyAnimation_37->start();
                        Register_QPropertyAnimation_21->start();
                        Register_QPropertyAnimation_19->start();

                        timer->stop();
                        delete timer;
                        current_end_animation--;
                    }else{
                        current_end_animation--;
                    }
                });

            }
        }

        std::cout<<"clicked"<<std::endl;
    }


}



void MainWindow::on_password_visible_pushButton_clicked()
{
    QIcon icon1(":/icon/eye_close_fixed.jpg");
    QIcon icon2(":/icon/eye_open_fixed.jpg");

    static int password_visible_pushButton_stat = 0;
    if(password_visible_pushButton_stat == 1){
        ui->password_visible_pushButton->setStyleSheet("QPushButton {border:0;"
                                                       "cursor: pointer;"
                                                       "background-image:url(:/icon/eye_close_fixed.jpg);"
                                                       "background-color:rgb(0,0,0,0);"
                                                       "border-radius:10px;}"
                                                       );
        password_visible_pushButton_stat = 0;
        ui->password_lineEdit->setEchoMode(QLineEdit::Password);
    }else{
        ui->password_visible_pushButton->setStyleSheet("QPushButton {border:0;"
                                                       "cursor: pointer;"
                                                       "background-image:url(:/icon/eye_open_fixed.jpg);"
                                                       "background-color:rgb(0,0,0,0);"
                                                       "border-radius:10px;}"
                                                       );
        password_visible_pushButton_stat = 1;
        ui->password_lineEdit->setEchoMode(QLineEdit::Normal);
    }

}

void MainWindow::mousePressEvent(QMouseEvent* event)
{
    mouse_pos = event->globalPos();
    window_pos = this->pos();
    diff_pos = mouse_pos - window_pos;
}

void MainWindow::mouseMoveEvent(QMouseEvent* event)
{
    QPoint pos = event->globalPos();
    //this->move(pos);
    this->move(pos - diff_pos);
}


void MainWindow::on_Quit_pushButton_clicked()
{
    this->close();
}


void MainWindow::on_MInimize_pushButton_clicked()
{
    this->setWindowFlags(Qt::Window|Qt::FramelessWindowHint|Qt::WindowMinimizeButtonHint);
    this->showNormal();
    this->showMinimized();
}


void MainWindow::on_Register_Button_clicked()
{
    if(registerOrLogin_stat)
    {
        Register_QPropertyAnimation_1->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_2->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_3->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_4->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_5->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_6->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_7->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_8->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_9->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_10->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_11->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_12->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_13->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_14->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_15->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_16->setDirection(QAbstractAnimation::Forward);



        Register_QPropertyAnimation_1->start();
        Register_QPropertyAnimation_2->start();
        Register_QPropertyAnimation_3->start();
        Register_QPropertyAnimation_4->start();
        Register_QPropertyAnimation_5->start();
        Register_QPropertyAnimation_6->start();
        Register_QPropertyAnimation_7->start();
        Register_QPropertyAnimation_8->start();
        Register_QPropertyAnimation_9->start();
        Register_QPropertyAnimation_10->start();
        Register_QPropertyAnimation_11->start();
        Register_QPropertyAnimation_12->start();
        Register_QPropertyAnimation_13->start();
        Register_QPropertyAnimation_14->start();
        Register_QPropertyAnimation_15->start();
        Register_QPropertyAnimation_16->start();

        ui->Register_Button->setText("Confirm");

        registerOrLogin_stat = 0;
    }else{
        QString register_username = ui->register_username_lineEdit->text();
        QByteArray register_username_ByteArray = register_username.toLatin1();
        char *register_username_char_arr = register_username_ByteArray.data();

        client_socket = socketAndSSL::Creat_client_socket();

        socketAndSSL::Connet_to_server(client_socket);

        ssl = socketAndSSL::Sync_init_ssl(client_socket);

        socketAndSSL::Ssl_connect(client_socket, ssl);

        char buffer[1024];
        memset(buffer, 0, sizeof(buffer));

        int ret = socketAndSSL::Register_request(buffer, ssl, register_username_char_arr, 1024);
        if(ret == 1){
            static int current_end_animation = 0;

            ui->OKorREJECT_tip_label->setText("Legal username");

            Register_QPropertyAnimation_17->setDirection(QAbstractAnimation::Forward);
            Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Forward);
            Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Forward);
            Register_QPropertyAnimation_22->setDirection(QAbstractAnimation::Forward);



            Register_QPropertyAnimation_17->start();
            Register_QPropertyAnimation_18->start();
            Register_QPropertyAnimation_19->start();
            Register_QPropertyAnimation_22->start();


            QTimer *timer = new QTimer(this);
            timer->start(3000);

            current_end_animation++;

            connect(timer,&QTimer::timeout,this,[=]{
                if(current_end_animation == 1){
                    Register_QPropertyAnimation_17->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_22->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_23->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_24->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_25->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_26->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_27->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_28->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_29->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_30->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_31->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_32->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_33->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_14->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_34->setDirection(QAbstractAnimation::Forward);
                    Register_QPropertyAnimation_35->setDirection(QAbstractAnimation::Forward);


                    Register_QPropertyAnimation_17->start();
                    Register_QPropertyAnimation_18->start();
                    Register_QPropertyAnimation_19->start();
                    Register_QPropertyAnimation_22->start();
                    Register_QPropertyAnimation_23->start();
                    Register_QPropertyAnimation_24->start();
                    Register_QPropertyAnimation_25->start();
                    Register_QPropertyAnimation_26->start();
                    Register_QPropertyAnimation_27->start();
                    Register_QPropertyAnimation_28->start();
                    Register_QPropertyAnimation_29->start();
                    Register_QPropertyAnimation_30->start();
                    Register_QPropertyAnimation_31->start();
                    Register_QPropertyAnimation_32->start();
                    Register_QPropertyAnimation_33->start();
                    Register_QPropertyAnimation_14->start();
                    Register_QPropertyAnimation_34->start();
                    Register_QPropertyAnimation_35->start();

                    timer->stop();
                    delete timer;
                    current_end_animation--;
                }else{
                    current_end_animation--;
                }
            });




            //success
        }else{
            static int current_end_animation = 0;

            ui->OKorREJECT_tip_label->setText("Illegal username");

            Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Forward);
            Register_QPropertyAnimation_20->setDirection(QAbstractAnimation::Forward);
            Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Forward);
            Register_QPropertyAnimation_21->setDirection(QAbstractAnimation::Forward);

            Register_QPropertyAnimation_18->start();
            Register_QPropertyAnimation_20->start();
            Register_QPropertyAnimation_19->start();
            Register_QPropertyAnimation_21->start();


            QTimer *timer = new QTimer(this);
            timer->start(3000);

            current_end_animation++;

            connect(timer,&QTimer::timeout,this,[=]{
                if(current_end_animation == 1){
                    Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_20->setDirection(QAbstractAnimation::Backward);
                    Register_QPropertyAnimation_21->setDirection(QAbstractAnimation::Backward);


                    Register_QPropertyAnimation_18->start();
                    Register_QPropertyAnimation_19->start();
                    Register_QPropertyAnimation_20->start();
                    Register_QPropertyAnimation_21->start();


                    timer->stop();
                    delete timer;
                    current_end_animation--;
                }else{
                    current_end_animation--;
                }
            });

            //fail
        }
    }

}


void MainWindow::on_backToNameMaking_pushButton_clicked()
{
    Register_QPropertyAnimation_23->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_24->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_25->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_26->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_27->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_28->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_29->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_30->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_31->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_32->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_33->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_34->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_35->setDirection(QAbstractAnimation::Backward);
    Register_QPropertyAnimation_14->setDirection(QAbstractAnimation::Forward);


    Register_QPropertyAnimation_23->start();
    Register_QPropertyAnimation_24->start();
    Register_QPropertyAnimation_25->start();
    Register_QPropertyAnimation_26->start();
    Register_QPropertyAnimation_27->start();
    Register_QPropertyAnimation_28->start();
    Register_QPropertyAnimation_29->start();
    Register_QPropertyAnimation_30->start();
    Register_QPropertyAnimation_31->start();
    Register_QPropertyAnimation_32->start();
    Register_QPropertyAnimation_33->start();
    Register_QPropertyAnimation_34->start();
    Register_QPropertyAnimation_35->start();
    Register_QPropertyAnimation_14->start();

}


void MainWindow::on_register_password_visible_pushButton_clicked()
{
    static int register_password_visible_pushButton_stat = 0;
    if(register_password_visible_pushButton_stat == 1){
        ui->register_password_visible_pushButton->setStyleSheet("QPushButton {border:0;"
                                                       "cursor: pointer;"
                                                       "background-image:url(:/icon/eye_close_fixed.jpg);"
                                                       "background-color:rgb(0,0,0,0);"
                                                       "border-radius:10px;}"
                                                       );
        register_password_visible_pushButton_stat = 0;
        ui->register_password_lineEdit->setEchoMode(QLineEdit::Password);
    }else{
        ui->register_password_visible_pushButton->setStyleSheet("QPushButton {border:0;"
                                                       "cursor: pointer;"
                                                       "background-image:url(:/icon/eye_open_fixed.jpg);"
                                                       "background-color:rgb(0,0,0,0);"
                                                       "border-radius:10px;}"
                                                       );
        register_password_visible_pushButton_stat = 1;
        ui->register_password_lineEdit->setEchoMode(QLineEdit::Normal);
    }
}


void MainWindow::on_compelet_Register_Button_clicked()
{    
    QString input_register_password_Qstr = ui->register_password_lineEdit->text();
    QByteArray input_register_password_ByteArray = input_register_password_Qstr.toLatin1();
    char *register_password_char_arr = input_register_password_ByteArray.data();


    static char buffer[1024];
    memset(buffer, 0, sizeof(buffer));
    int ret = socketAndSSL::Register_set_password(buffer, ssl, register_password_char_arr, 1024);
    /* Value under -1 in if is TRUE!!!??? */
    if(ret == 1){
        //success
        static int current_end_animation = 0;

        ui->OKorREJECT_tip_label->setText("legal password");

        Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_17->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_22->setDirection(QAbstractAnimation::Forward);

        Register_QPropertyAnimation_18->start();
        Register_QPropertyAnimation_19->start();
        Register_QPropertyAnimation_17->start();
        Register_QPropertyAnimation_22->start();

        QTimer *timer = new QTimer(this);
        timer->start(3000);

        current_end_animation++;

        connect(timer,&QTimer::timeout,this,[=]{
            if(current_end_animation == 1){
                Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Backward);
                Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Backward);
                Register_QPropertyAnimation_17->setDirection(QAbstractAnimation::Backward);
                Register_QPropertyAnimation_22->setDirection(QAbstractAnimation::Backward);

                Register_QPropertyAnimation_18->start();
                Register_QPropertyAnimation_19->start();
                Register_QPropertyAnimation_17->start();
                Register_QPropertyAnimation_22->start();

                timer->stop();
                delete timer;
                current_end_animation--;
            }else{
                current_end_animation--;
            }
        });
    }else{
        //fail
        static int current_end_animation = 0;

        ui->OKorREJECT_tip_label->setText("Illegal password");

        Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_20->setDirection(QAbstractAnimation::Forward);
        Register_QPropertyAnimation_21->setDirection(QAbstractAnimation::Forward);

        Register_QPropertyAnimation_18->start();
        Register_QPropertyAnimation_19->start();
        Register_QPropertyAnimation_20->start();
        Register_QPropertyAnimation_21->start();

        QTimer *timer = new QTimer(this);
        timer->start(3000);

        current_end_animation++;

        connect(timer,&QTimer::timeout,this,[=]{
            if(current_end_animation == 1){
                Register_QPropertyAnimation_18->setDirection(QAbstractAnimation::Backward);
                Register_QPropertyAnimation_19->setDirection(QAbstractAnimation::Backward);
                Register_QPropertyAnimation_20->setDirection(QAbstractAnimation::Backward);
                Register_QPropertyAnimation_21->setDirection(QAbstractAnimation::Backward);

                Register_QPropertyAnimation_18->start();
                Register_QPropertyAnimation_19->start();
                Register_QPropertyAnimation_20->start();
                Register_QPropertyAnimation_21->start();

                timer->stop();
                delete timer;
                current_end_animation--;
            }else{
                current_end_animation--;
            }
        });

    }
    memset(buffer, 0, sizeof(buffer));
}

